#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iterator>
#include <set>
#include <cctype>

using namespace std;

void display_words(const string &s)
{
    static int count;
    count++;
    cout << count << ": " << s << endl;
}

string to_lower(const string &s)
{
    transform(s.begin(), s.end(), s.begin(), toLower);
    return s;
}

char toLower(char ch)
{
    return tolower(ch);
}

int main(int argc, char const *argv[])
{
    // 输入
    vector<string> words;
    cout << "Enter words for input into vector, \"exit\" for exit\n";
    string temp;
    while (cin >> temp && temp != "exit")
    {
        words.push_back(temp);
    }
    // 遍历输出
    cout << "You have input the following words: \n";
    for_each(words.begin(), words.end(), display_words);
    cout << endl;

    // 加入到set中去重
    // 预处理
    set<string> wordset;
    transform(words.begin(),words.end(),insert_iterator<set<string>>(wordset,wordset.begin()), to_lower);       // 直接使用迭代器会因为没有容量而崩溃,这里使用插入迭代器
    cout << "\nAlphabetic list of words:\n";
    for_each(wordset.begin(), wordset.end(), display_words);
    cout << endl;

    // place word and frequency in map
    map<string, int> wordmap;
    set<string>::iterator si;
    for (si = wordset.begin(); si != wordset.end(); si++)
        wordmap[*si] = count(words.begin(), words.end(), *si);
        // 通过map中的[]可以直接传入键进行查找

    // display map contents
    cout << "\nWord frequency:\n";
    for (si = wordset.begin(); si != wordset.end(); si++)
        cout << *si << ": " << wordmap[*si] << endl;
    // cin.get();
    // cin.get();

    return 0;
}
